<?php 
//localhost/restful-api-demo/restful/users/

require_once('../lib/users.php');
require '../vendor/autoload.php';

use lib\users\users;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;



/*
* index.php 作为接口统一入口
*/
class Restful{

//用户资源模块
private $_users ;
//错误日志记录
private $_errLog;
//错误日志路径 默认当前路径
private $_errLogPath = 'app.log';
//请求方式
private $_requestMethod;
//请求资源
private $_requestResources;
//id
private $_id;
//允许请求的资源范围
private $_allowRequestResources = ['users','contents'];

//允许请求的请求方式
private $_allowRequestMethod = ['POST','GET','PUT','DELETE'];
//常用状态吗
private $_statusCodes = [
	200=>'ok',
	204=>'No Content', //空的
	400=>'Bad Request',//
	401=>'Unauthorized',//非法的
	403=>'Forbidden', //被禁止
	404=>'Not Found',
	405=>'Method Not Allowed',
	500=>'Server Internal Error'

];

//加载用户资源模块
public function __construct($obj){
	$this->_users = $obj;
	$this->_eLog = new Logger('names');
	$this->_eLog->pushHandler(new StreamHandler($this->_errLogPath, Logger::WARNING));


}
//运行方法
public function run(){

	try {
		$this->_setRequestMethod();
		$this->_setRequestResources();

		switch ($this->_requestResources) {
			case 'users':
				//调用请求资源模块方法 users
				$this->_json($this->_handleUser(),200);
				break;
			case 'contents':
				//调用请求资源模块方法 contents
				$this->_json($this->_handleContents(),200);
				break;
			default:
				# code...
				break;
		}
		

		
	
	} catch (Exception $e) {
		$this->_eLog->addError("入口文件请求出错| ".$_SERVER['SCRIPT_FILENAME']." ",array("code"=>$e->getCode(),'message'=>$e->getMessage()));
		$this->_json(['error'=>$e->getMessage(),'code'=>$e->getCode()],$e->getCode());

	}
}
//获取请求类型
private function _setRequestMethod(){
	$this->_requestMethod = $_SERVER['REQUEST_METHOD'];
	if(!in_array($this->_requestMethod, $this->_allowRequestMethod)){
		throw new Exception("请求类型不允许", 405);
	}
}
//获取请求资源名称
private function _setRequestResources(){
	$path = $_SERVER['PATH_INFO'];
	$path = explode('/', $path);
	$this->_requestResources = $path[1];
	$this->_id = $path[2];
	if(!in_array($this->_requestResources, $this->_allowRequestResources)){
		throw new Exception("请求资源出错", 400);
		
	}
}
//输出Json
private function _json($arr,$code = 0){
	if($code !=200){
		header("HTTP/1.1 ".$code." ".$this->_statusCodes[$code]);
	}
	header('Content-Type:application/json;charset=utf-8');
	echo json_encode($arr,JSON_UNESCAPED_UNICODE);
	exit();
}
//处理用户资源模块方法
private function _handleUser(){
	if($this->_requestMethod == 'POST'){
		$data = $_POST;
		if(isset($data['act'])){
			switch ($data['act']) {
				case 'login': //用户资源的登录方法
					return $this->_users->login($data['username'],$data['pwd']);
					break;
				case 'reg': //用户资源的注册方法
					return $this->_users->register($data['username'],$data['pwd']);
					break;
				default:
					# code...
					break;
			}
		}
	}else{
		throw new Exception("请求类型不允许", 405);
	}
}


//处理内容资源的模块方法
private function _handleContents(){
	//###### gaoyang
}
}

$users = new users();
$restful = new Restful($users);
$restful->run(); //跑起来~~~~~~~~~~~~~
